package com.scau.database.finalexam.controllers;

import com.scau.database.finalexam.DAO.UsersDao;
import com.scau.database.finalexam.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;


import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    UsersDao usersDao;

//    @RequestMapping(value= "/user/login",method = RequestMethod.POST)
    @PostMapping(value = "/user/login")
    public String login(User user,
                        Map<String,Object> map,
                        HttpSession session){
        System.out.println(user);
        User temp = usersDao.get(user.getId());
        System.out.println(temp);
        if(temp.equals(user)){
            session.setAttribute("loginUser",temp);
            //防止表单重复提交，使用重定向
            return "redirect:/main.html";
        } else if(temp==null){
            map.put("msg","不存在此用户");
        } else {
            map.put("msg", "用户名密码错误");
        }
            return "login";
    }


}
